package com.haois.component.util.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

/**
 * description
 *
 * @author RuiHaoZ on GCU
 * @date 2017/3/17 9:18
 */
public class ExcelCellStyle {
    private HSSFWorkbook workbook;

    public ExcelCellStyle(HSSFWorkbook workbook) {
        this.workbook = workbook;
    }

    public HSSFCellStyle headStyle() {
        HSSFFont font = workbook.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        font.setFontHeightInPoints((short) 12);

        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
        cellStyle.setFillBackgroundColor(HSSFColor.YELLOW.index);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 左右居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 上下居中
        cellStyle.setFont(font);

        return cellStyle;
    }

    public HSSFCellStyle alignStyle() {
        HSSFFont font = workbook.createFont();


        HSSFCellStyle cellStyle = workbook.createCellStyle();

        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setFillForegroundColor(HSSFColor.BROWN.index);
        cellStyle.setFillBackgroundColor(HSSFColor.BROWN.index);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 左右居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 上下居中
        cellStyle.setFont(font);
        return cellStyle;
    }

    public HSSFCellStyle textStyle() {

        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFDataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));
        return cellStyle;
    }

    public HSSFSheet setLotCell(HSSFSheet sheet, int firstRow, int lastRow, int firstCol, int lastCol, HSSFCellStyle style) {
        for (int i = firstRow; i < lastRow; i++) {
            HSSFRow row = sheet.createRow(i);
            for (int j = firstCol; j < lastCol; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellStyle(style);
            }
        }
        return sheet;
    }
}
